package com.ruoyi.teaching.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.common.constant.HttpStatus;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.li.work.mapper.SysUserMapper;
import com.ruoyi.teaching.domain.WebSocketTemporaryMessage;
import com.ruoyi.teaching.mapper.WebSocketTemporaryMessageMapper;
import com.ruoyi.teaching.service.WebSocketTemporaryMessageService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author 李志鑫
 * @since 2024-09-10
 */
@Service
public class WebSocketTemporaryMessageServiceImpl extends ServiceImpl<WebSocketTemporaryMessageMapper, WebSocketTemporaryMessage> implements WebSocketTemporaryMessageService {
    @Autowired
    private WebSocketTemporaryMessageMapper webSocketTemporaryMessageMapper;

    @Autowired
    private SysUserMapper sysUserMapper;


    @Override
    public AjaxResult getTemporaryMessageList(Integer pageNum, Integer pageSize) {
        Page<WebSocketTemporaryMessage> webSocketTemporaryMessagePage = new Page<>(pageNum, pageSize);
        Long userId = SecurityUtils.getUserId();
        QueryWrapper<WebSocketTemporaryMessage> webSocketTemporaryMessageQueryWrapper = new QueryWrapper<>();
        webSocketTemporaryMessageQueryWrapper.eq("user_a",userId).or().eq("user_b",userId);
        webSocketTemporaryMessageQueryWrapper.orderByDesc("create_time");
        Page<WebSocketTemporaryMessage> webSocketTemporaryMessagePage1 = webSocketTemporaryMessageMapper.selectPage(webSocketTemporaryMessagePage, webSocketTemporaryMessageQueryWrapper);
        return new AjaxResult(HttpStatus.SUCCESS,"查询成功",webSocketTemporaryMessagePage1);
    }

    @Override
    public AjaxResult getTemporaryMessageAdd(WebSocketTemporaryMessage webSocketTemporaryMessage) {
        Long userId = SecurityUtils.getUserId();
        QueryWrapper<WebSocketTemporaryMessage> webSocketTemporaryMessageQueryWrapper = new QueryWrapper<>();
        webSocketTemporaryMessageQueryWrapper.eq("user_a",userId);
        webSocketTemporaryMessageQueryWrapper.eq("user_b",webSocketTemporaryMessage.getUserB());
        Integer aLong = Math.toIntExact(webSocketTemporaryMessageMapper.selectCount(webSocketTemporaryMessageQueryWrapper));
        if (aLong>=5) {
            return new AjaxResult(HttpStatus.ERROR,"临时消息达到5次");
        }
        SysUser sysUser = sysUserMapper.selectUserById(userId);
        webSocketTemporaryMessage.setUserA(userId);
        webSocketTemporaryMessage.setUserAAvatar(sysUser.getAvatar());
        webSocketTemporaryMessage.setUserANickName(sysUser.getNickName());
        if (null!=webSocketTemporaryMessage.getUserB()&&!webSocketTemporaryMessage.getUserB().equals("")) {
            SysUser sysUser1 = sysUserMapper.selectUserById(webSocketTemporaryMessage.getUserB());
            webSocketTemporaryMessage.setUserB(sysUser1.getUserId());
            webSocketTemporaryMessage.setUserBAvatar(sysUser1.getAvatar());
            webSocketTemporaryMessage.setUserBNickName(sysUser1.getNickName());
        }else {
            return new AjaxResult(HttpStatus.ERROR,"消息格式错误");
        }
        Integer insert = webSocketTemporaryMessageMapper.insert(webSocketTemporaryMessage);
        if (insert == 0) {
            return new AjaxResult(HttpStatus.ERROR,"发送失败");
        }
        return new AjaxResult(HttpStatus.SUCCESS,"发送成功");
    }

    @Override
    public AjaxResult listByUserId(Integer userIdB) {
        Long userIdA = SecurityUtils.getUserId();
        QueryWrapper<WebSocketTemporaryMessage> webSocketTemporaryMessageQueryWrapper = new QueryWrapper<>();
        webSocketTemporaryMessageQueryWrapper.eq("user_a", userIdA).eq("user_b",userIdB).or(w->w.eq("user_b", userIdA).eq("user_a",userIdB));
        webSocketTemporaryMessageQueryWrapper.orderByAsc("create_time");
        List<WebSocketTemporaryMessage> webSocketTemporaryMessages = webSocketTemporaryMessageMapper.selectList(webSocketTemporaryMessageQueryWrapper);
        return new AjaxResult(HttpStatus.SUCCESS,"成功",webSocketTemporaryMessages);
    }

}
